<?php
use models\Factory;
use util\CommonUtil;
include '../autoload.php';
require_once('../vendor/autoload.php');

header("Content-Type: text/html; charset=UTF-8");
ini_set('memory_limit','2650M');

//抓取第三方评论数据........
set_time_limit(0);

$ua = CommonUtil::getPcUserAgent();
if (empty($ua)) {
    CommonUtil::echoStr("\nUA初始化失败，执行下次任务\n");
    exit();
}

try {

    $productStr = CommonUtil::curl('https://www.uodian.com/api/comment/get-job/');
    if (empty($productStr)) {  //没有任务时，休息10分钟，再进行下一个任务
        CommonUtil::echoStr("\n没有任务，中断\n");
        exit();
    }
    $productArr = explode('[][]',$productStr);
    //var_dump($productArr);
    $urls = explode(',',$productArr[1]);  //要抓取的网页URL列表
    //var_dump($urls);

    foreach ($urls as $url) {

        CommonUtil::echoStr("\n抓取页面：{$url}\n");

        $spider = Factory::getSpider($url, $ua);
        $comments = $spider->getComments(); //评论列表

        $post_data = array();
        if (!empty($comments)) {

            /*foreach ($comments as $comment) {
                CommonUtil::echoStr("\n-----------------------------------------\n");
                CommonUtil::echoStr($comment["content"]);
                CommonUtil::echoStr("\n-----------------------------------------\n");
            }*/


            $post_data['product_id'] = $productArr[0];
            $post_data['spider_url'] = $url;
            $post_data['list'] = json_encode($comments);
            $str = CommonUtil::curl('https://www.uodian.com/api/comment/add-comment/', $post_data);
            CommonUtil::echoStr("\n{$str}\n");
            echo 'haha';
        }else{
            CommonUtil::echoStr("\n该页面没有评论数据，忽略此任务\n");
        }
    }

} catch (\Exception $e) {

    CommonUtil::logException($e);   //记录异常信息
    CommonUtil::echoStr($msg = "\n发生异常，过滤本次任务~\n");
}

//释放内存
if (!empty($productStr)) unset($productStr);
if (!empty($productArr)) unset($productArr);
if (!empty($spider)) unset($spider);
if (!empty($comments)) unset($comments);
if (!empty($post_data)) unset($post_data);


?>